package com.lsq.baseadmin.modules.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lsq.baseadmin.modules.manage.entity.Position;
import com.lsq.baseadmin.modules.manage.mapper.PositionMapper;
import com.lsq.baseadmin.modules.manage.service.PositionService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lsq
 * @since 2020-10-27
 */
@Service
public class PositionServiceImpl extends ServiceImpl<PositionMapper, Position> implements PositionService {

    @Override
    public List<Position> list(Position position) {
        QueryWrapper<Position> wrapper = createWrapper(position);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public PageInfo pg(Position position, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Position> list = list(position);
        return new PageInfo<>(list);
    }

    private QueryWrapper<Position> createWrapper(Position position) {
        QueryWrapper<Position> wrapper = new QueryWrapper<>();
        if(null == position) {
            return wrapper;
        }

        if(StringUtils.hasText(position.getName())) {
            wrapper.like("name", position.getName());
        }

        if(position.getDepartmentId() != null) {
            wrapper.eq("department_id", position.getDepartmentId());
        }

        if(position.getStatus() != null) {
            wrapper.eq("status", position.getStatus());
        }

        return wrapper;
    }
}
